home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Tools / glimpsehttp / cgi-bin / news < prev   
Text File  |  1995-05-16  |  6KB  |  229 lines

  1. #!/usr/local/bin/perl
  2. #
  3. # Acknowledgements
  4. #
  5. # Thanks to Guy Brooker (guy@jw.estec.esa.nl) for his AA interface,
  6. # which was the starting point for this program.
  7. #
  8. # Paul Clark
  9. # paul@cs.arizona.edu
  10. #
  11. # Modifications
  12. # 2/22/94    Version 1.0, shell script version    Paul Clark
  13. # 4/21/94    Version 1.1, multiple archives support    Paul Clark
  14. # 4/22/94    Version 1.2, perl script        Paul Clark
  15. # 4/26/94    rewritten from aglimpse script
  16.  
  17. # **** **** **** ****    CONFIGURABLE VARIABLES     **** **** **** ****
  18. $HTTPD_HOME="/usr1/paul/httpd" ;
  19. $HTTPD_NEWSHOME="/usr1/paul/news" ;
  20. $GLIMPSE_LOC="/usr/paul/bin/glimpse" ;
  21.  
  22. # **** **** **** **** NO CONFIGURATION NEEDED BELOW **** **** **** ****
  23.  
  24. $FSSERV="/cgi-bin/article" ;
  25.  
  26. #    To support an ISINDEX type search, set query string if given
  27. #    an argument on the command line
  28. if ( $#ARGV >= 0 ) {
  29.     $prefix= "whole=on&case=on&query=";
  30. }
  31.  
  32. if ( $ENV{'PATH_INFO'} ) {
  33. # old-fashioned way to give newsgroup
  34.     $newsgroup = substr($ENV{'PATH_INFO'},1);
  35.     $newsgroup =~ s|"||g;
  36.     $prefix= "group=" . $newsgroup . "&". $prefix;
  37. }
  38.  
  39. #    Check that a query has been made
  40. $query = $ENV{'QUERY_STRING'};
  41. $query =~ s|"||g;
  42.  
  43. #    Strip the variables out from the query string,
  44. #    and assign them into variables, prefixed by 'QS_'
  45. @qvars = split( /\&/, $prefix . $query );
  46. #print "Content-type: text/plain\n\n" ; #debug
  47. foreach (@qvars) {
  48.     split(/=/);
  49.     $fname = $_[0];
  50.     $fvalue = $_[1];
  51.     $cmd = "\$QS_$fname = \"$fvalue\";" ;
  52. #    print "$cmd\n"; #debug
  53.     $cmd = eval $cmd if ( $fname =~ /^[a-z_A-Z]\w*$/ );
  54. }
  55.  
  56. $newsgroup = $QS_group;
  57. $newsgroup =~ tr/A-Z/a-z/;
  58. $indexdir = $HTTPD_NEWSHOME . "/indices/" . $newsgroup;
  59. $ENV{'HOME'} = $indexdir; # some versions of Glimpse need it
  60.  
  61. # Ensure that Glimpse is available on this machine
  62. -x $GLIMPSE_LOC || &err_noglimpse ;
  63.  
  64. # Ensure that index is available
  65. -r "$indexdir/.glimpse_index" || &err_noindex ;
  66.  
  67. $QS_query =~ s|\+| |g;
  68. $QS_query =~ s/%(\w\w)/sprintf("%c", hex($1))/ge;
  69. $QS_query =~ s|"||g;
  70.  
  71. #if no query has been made, provide ISINDEX type of reply
  72. $QS_query || &err_noquery ;
  73.  
  74. $OPT_errors="-$QS_errors"    if $QS_errors =~ /^[0-8]$/;
  75. $OPT_errors="-B -y"        if $QS_errors =~ /^Best\+match$/;
  76. $OPT_case="-i"            if $QS_case =~ /^on$/;
  77. $OPT_whole="-w"            unless $QS_whole =~ /^on$/;
  78.  
  79. if ($QS_maxlines =~ /\d+/) {
  80.     $maxlines = $&;
  81. } else {
  82.     $maxlines = 20;
  83. }
  84. if ($QS_maxfiles =~ /\d+/) {
  85.     $maxfiles = $&;
  86. } else {
  87.     $maxfiles = 100;
  88. }
  89.  
  90. print "Content-type: text/html\n\n" ;
  91. print "<HEAD><TITLE>Search for \"$QS_query\" in \"$newsgroup\"\n";
  92. print "</TITLE></HEAD><BODY>\n";
  93. print "<H1>Glimpse search for \"$QS_query\"<BR>".
  94.     "in newsgroup \"$newsgroup\"</H1><HR>\n";
  95.  
  96. chdir $indexdir;
  97. unlink <.glimpse_tmp*> ;
  98. $cmd = "exec $GLIMPSE_LOC -z -y -n $OPT_case $OPT_whole $OPT_errors -H ." .
  99.      "$OPT_filter \"$QS_query\" 2>&1 |";
  100. # print $cmd,"\n",`pwd`;
  101. $gpid = open(GOUT, $cmd );
  102. $prevfile = "";
  103. $lcount = 0;
  104. $fcount = 0;
  105. line: while (<GOUT>) {
  106.     s/&/\&/g;
  107.     s/</\</g;
  108.     s/>/\>/g;
  109.     ( /^([^ :]*):\s*([0-9][0-9]*):(.*)/ ) || next;
  110.     $file = $1;
  111.     $line = $2;
  112.     $string = $3;
  113.     $file =~ s|.*groups(/[^/]*/[^/]*)$|$1|;
  114.     if ($file ne $prevfile) {
  115.         $fcount++ ;
  116.         $linecount = -1;
  117.         if ($fcount>$maxfiles) {
  118.             print "<H3>Limit of $maxfiles " .
  119.                 "articles exceeded...</H3>\n";
  120.             $file = "";
  121.             $fcount = "at least $fcount";
  122.             $lcount = "at least $lcount";
  123.             last line;
  124.         }
  125.         print "</UL>" if ( $prevfile ne "" );
  126.         $prevfile = $file ;
  127.         print "<H3>Article <A HREF=\"",$FSSERV,$file,
  128.             "\">",$file,"</A></H3><UL>\n" ;
  129.     }
  130.     $lcount++ ;
  131.     $linecount++;
  132.     if ($linecount>=$maxlines) {
  133.         print "<LI>Limit of $maxlines matched " .
  134.             "lines per file exceeded...\n" if
  135.                 $linecount==$maxlines;
  136.         next line;
  137.     }
  138.     print "<LI><A HREF=\"",$FSSERV,$file,"?",$line,"#mfs\">\n" ;
  139.     print "line ",$line,":",$string,"</A>\n" ;
  140. }
  141. print "</UL>\n" if $file ;
  142. print "<HR>" ;
  143. print "<H3>Summary for query <code>\"",$QS_query,"\"</code>:</H3>\n" ;
  144. print "found ",$lcount," matches in ",$fcount," articles\n" ;
  145. print "</BODY>\n" ;
  146. close(GOUT);
  147. unlink "/tmp/.glimpse_tmp.$gpid";
  148.  
  149. sub diag_exit {
  150. # exit on error
  151.     exit 1;
  152. }
  153. sub err_noquery {
  154. #    The script was called without a query. 
  155. #    Provide an ISINDEX type response for browsers
  156. #    without form support.
  157. print "Content-type: text/html\n\n";
  158. print "<HEAD><TITLE>Search newsgroup \"$newsgroup\"</TITLE></HEAD>\n";
  159. print "<BODY><H1> Search newsgroup \"$newsgroup\" </H1>\n";
  160. print "Welcome to the gateway to \"$newsgroup\".\n";
  161.     print <<'EOM' ;
  162. Type a pattern to search in your browser's
  163. search dialog. Query is case-insensitive by default<P>
  164.  
  165. <ISINDEX>
  166.  
  167. <H2>IMPORTANT !</H2>
  168. <QUOTE>
  169. <UL>
  170. <LI>This is an experimental service. Therefore, its functionality
  171. may change or discontinue at any moment without prior notice.
  172. <LI>Please report any malfunctions of this gateway to the
  173. address below.
  174. <LI>All access operations on the server are logged.
  175. If you discover any security leaks, don't use them - report them to
  176. the address below.
  177. </UL>
  178. </QUOTE>
  179.  
  180. <HR>
  181. <ADDRESS>
  182. Your name<BR>
  183. login@your.host.here<BR>
  184. </ADDRESS>
  185.  
  186. </BODY>
  187.  
  188. EOM
  189.     &diag_exit;
  190. }
  191.  
  192. sub err_noglimpse {
  193. #
  194. # Glimpse was not found
  195. # Report a useful message
  196. #
  197.     print <<'EOM' ;
  198. Content-type: text/html
  199.  
  200. <HEAD>
  201. <TITLE>Glimpse not found</TITLE>
  202. </HEAD>
  203. <BODY>
  204. <H1>Glimpse not found</H1>
  205.  
  206. This gateway relies on <CODE>Glimpse</CODE> search tool.
  207. If it is installed, please set the correct path in the script file.
  208. Otherwise obtain the latest version from
  209. <A HREF="file://ftp.cs.arizona.edu/glimpse">ftp.cs.arizona.edu</A>
  210. </BODY>
  211. EOM
  212.     &diag_exit;
  213. }
  214.  
  215. sub err_noindex {
  216. print "Content-type: text/html\n\n";
  217. print "<HEAD><TITLE>Newsgroup $newsgroup not found</TITLE>\n";
  218. print "</HEAD> <BODY>\n";
  219. print "<H1>Newsgroup $newsgroup not found</H1>\n";
  220.     print <<'EOM' ;
  221. Please remember: this is NOT a news server. It has only a few
  222. newsgroups. Please use it ONLY for the search of a particular article,
  223. NOT for reading the news.
  224. </BODY>
  225. EOM
  226.     &diag_exit;
  227. }
  228.